<template>
  <div class="container">
    <div class="search-box">
      <el-input v-model="search" placeholder="请输入内容" class="input-box"></el-input>
      <el-button @click="qingchu">清除</el-button>
      <el-button type="primary" @click="ss">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="list">
      <el-table-column type="index" label="任务编号" width="80" :index="(params._page-1)*params._limit+1">
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button size="mini" style="margin-right: 10px" @click="update(row.id)">编辑</el-button>
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="shangchu(row.id)"
          >
            <template #reference>
              <el-button  size="mini" type="danger" >删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination layout="prev, pager, next" 
            :current-page="params._page"
            :total="total"
            :page-size="params._limit"
            @current-change="pageChange"> </el-pagination>
      <el-button size="small" plain @click="openDialog">+ 新增</el-button>
    </div>
    <el-dialog :title="this.form.id ? '编辑' : '新增'" :visible="dialogVisible" width="30%" @close="closeDialog">
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="任务描述" prop="des">
          <el-input v-model="form.des"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {win,remove,Added,editor,xiangqing,searchAPI} from '@/api.js'
export default {
  data() {
    return {
      list:[],
       params: {
          _page: 1,  // 当前第几页
          _limit: 5  // 每页条数
        },
        total:0,
        dialogVisible: false,
        form:{
        name: '',
        des: '',
        id: ''
        },
        search: '',
   rules: {
        name: [{ required: true, message: '请输入任务名称', trigger: 'blur' }],
        des: [{ required: true, message: '请输入任务描述', trigger: 'blur' }]
      },

    }
  },
  mounted(){
      this.List()
  },
  methods:{
    async List() {
    const res = await win(this.params)
      // console.log(res);
      this.list=res.data
      this.total=+(res.headers['x-total-count'])
    },
    pageChange(page) {
      // newPage是当前点击的页码
      this.params._page = page // 将当前页码赋值给当前的最新页码
      this.List()
    },
    // 删除
   async shangchu(id){
   await remove(id)
       this.List()
    } ,
    // 新增
    openDialog () {
      this.dialogVisible = true
    },
    closeDialog () {
      this.dialogVisible = false
      this.$refs.form.resetFields()
         this.form = {
        name: '',
        des: '',
        id: ''
      }
    },
    confirm () {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.form.id) {
            await editor(this.form)
            this.$message.success('修改成功')
          } else {
            this.form.id = `${this.total+1}`
            await Added(this.form)
            this.$message.success('添加成功')
          }
          this.closeDialog()
          this.List()
        } else {
          this.$message.error('任务名称或者任务描述有误')
        }
      })
    },
    // 修改
    async update (id) {
      this.dialogVisible = true
      const res = await xiangqing(id)
      this.form = res.data
    },
    // 清除
    qingchu(){
      this.search = ''
      this.List()
    },
    // 搜索
  async ss(){
     const {data:res}=  await searchAPI(this.search)
    // console.log(res);
    this.list=res
   this.total=res.length
    }
  }
};
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
